package cn.com.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiU_user_friend_setting;

import cn.com.api.mapper.ApiU_user_friend_settingMapper;

import cn.com.api.service.ApiU_user_friend_settingService;
import cn.com.api.service.impl.base.U_user_friend_settingServiceImplBase;
import cn.com.util.Utils;

/**
 * 
 **/
@Service
public class ApiU_user_friend_settingServiceImpl extends U_user_friend_settingServiceImplBase
		implements ApiU_user_friend_settingService {
	@Autowired
	private ApiU_user_friend_settingMapper apiU_user_friend_settingMapper;

	// 新增对象后返回记录ID
	public int U_user_friend_settingInsertion(ApiU_user_friend_setting u_user_friend_setting) {
		return this.apiU_user_friend_settingMapper.U_user_friend_settingInsertion(u_user_friend_setting);
	}

	public ApiU_user_friend_setting getFriendSetting(String user_id, String friend_id) throws Exception {
		ApiU_user_friend_setting result = apiU_user_friend_settingMapper.getFriendSetting(user_id, friend_id);
		if (result == null) {
			result = new ApiU_user_friend_setting();
		}
		ApiU_user_friend_setting setting = apiU_user_friend_settingMapper.getFriendSetting(friend_id, user_id);
		if (setting != null) {
			result.setAdmin_is_readdynamic(setting.getIs_readdynamic());
		} else {
			result.setAdmin_is_readdynamic(0);
		}
		return result;
	}

	public Map<String, Object> updateUserFriendSetting(ApiU_user_friend_setting setting) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		if (setting.getId() == null || setting.getId() == 0) { // 新增
			apiU_user_friend_settingMapper.U_user_friend_settingInsertion(setting);
		} else {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("id", setting.getId());
			if (setting.getIs_neglectnoread() != null) {
				param.put("is_neglectnoread", setting.getIs_neglectnoread());
			}
			if (setting.getIs_nointerrupt() != null) {
				param.put("is_nointerrupt", setting.getIs_nointerrupt());
			}
			if (setting.getIs_open() != null) {
				param.put("is_open", setting.getIs_open());
			}
			if (setting.getIs_receivedel() != null) {
				param.put("is_receivedel", setting.getIs_receivedel());
			}
			if (setting.getIs_senddel() != null) {
				param.put("is_senddel", setting.getIs_senddel());
			}
			if (setting.getIs_top() != null) {
				param.put("is_top", setting.getIs_top());
			}
			if (setting.getIs_readdynamic() != null) {
				param.put("is_readdynamic", setting.getIs_readdynamic());
			}
			apiU_user_friend_settingMapper.updateUserFriendSettingByMap(param);
		}
		map.put(Utils.DATAS, setting);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "操作完成！");
		return map;
	}

	public void setisreaddynamic(String user_id, String friend_id, String is_readdynamic) throws Exception {
		ApiU_user_friend_setting friendSetting = apiU_user_friend_settingMapper.getFriendSetting(friend_id, user_id);
		if (friendSetting != null) {
			friendSetting.setIs_readdynamic(Integer.valueOf(is_readdynamic));
			apiU_user_friend_settingMapper.updateU_user_friend_setting(friendSetting);
		} else {
			friendSetting = new ApiU_user_friend_setting();
			friendSetting.setIs_readdynamic(Integer.valueOf(is_readdynamic));
			friendSetting.setUser_id(friend_id);
			friendSetting.setConn_id(user_id);
			apiU_user_friend_settingMapper.U_user_friend_settingInsertion(friendSetting);
		}
	}

}
